/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boogie.assetgenerator;

import boogie.assetgenerator.data.AssetFolder;
import boogie.assetgenerator.data.AssetsBuilder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Build",
id = "boogie.assetgenerator.GenerateClassAction")
@ActionRegistration(iconBase = "boogie/assetgenerator/icon.png",
displayName = "#CTL_GenerateClassAction")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 140, separatorBefore = 120, separatorAfter = 160),
    @ActionReference(path = "Toolbars/jMonkeyPlatform-Tools", position = -80),
    @ActionReference(path = "Shortcuts", name = "DO-A")
})
@Messages("CTL_GenerateClassAction=Generate Assets")
public final class GenerateClassAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        Project proj = OpenProjects.getDefault().getMainProject();
        if (proj != null) {
            try {
                FileObject root = proj.getProjectDirectory();
                final String path = root.getPath();
                File f = new File(path + "/assets");

                AssetFolder assetFolder = AssetsBuilder.getInstance().build(f);
    //            JCodeModel jcm = new JCodeModel();
    //            try {
    //                JDefinedClass mainClass = jcm._class(JMod.PUBLIC | JMod.FINAL, "generated.Assets", ClassType.CLASS);
    //
    //                buildClass(assetFolder, mainClass);
    //            } catch (JClassAlreadyExistsException ex) {
    //                Exceptions.printStackTrace(ex);
    //            }
    //            try {
    //                jcm.build(new CodeWriter() {
    //
    //                    private FileOutputStream fout;
    //
    //                    @Override
    //                    public void close() throws IOException {
    //                        fout.close();
    //                        System.out.println("Closing");
    //                    }
    //
    //                    @Override
    //                    public OutputStream openBinary(JPackage jp, String string) throws IOException {
    //                        File f = new File(path + "/src");
    //                        System.out.println(f.getPath());
    //                        File gen = new File(f, "generated");
    //                        f.mkdirs();
    //                        gen.mkdirs();
    //                        File assetClass = new File(gen, string);
    //
    //                        assetClass.createNewFile();
    //                        fout = new FileOutputStream(assetClass);
    //                        System.out.println(assetClass.getAbsolutePath());
    //                        return fout;
    //                    }
    //                });
    //            } catch (IOException ex) {
    //                Exceptions.printStackTrace(ex);
    //            }
                File file = new File(path + "/src");
                System.out.println(f.getPath());
                File gen = new File(file, "generated");
                file.mkdirs();
                gen.mkdirs();
                File assetClass = new File(gen, "assets.java");
                assetClass.createNewFile();
                
                
                String theStuff = "package generated;\n\n"+assetFolder.toString();
                PrintWriter bw = new PrintWriter(new FileWriter(assetClass, false));
                
                bw.println(theStuff);
                bw.flush();
                bw.close();
                System.gc();
                
               
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
              
                
            }
        }else{
            JOptionPane.showConfirmDialog(null, "Please set the project you would like to work with as the Main Project!", "No Main Project", JOptionPane.DEFAULT_OPTION);
        }

    }

//    private void buildClass(AssetFolder folder, JDefinedClass clazz) throws JClassAlreadyExistsException {
//        for (Asset a : folder.getAssets()) {
//            clazz.field(JMod.PUBLIC | JMod.FINAL | JMod.STATIC, String.class, a.getName(), JExpr.lit(a.getPath()));
//        }
//        for (AssetFolder f : folder.getFolders()) {
//            buildClass(f, clazz._class(JMod.PUBLIC | JMod.FINAL | JMod.STATIC, f.getName()));
//        }
//
//    }
}
